package com.dz.goods.service;

import com.alibaba.fastjson.JSON;
import com.dz.common.constants.CodeDomain;
import com.dz.common.core.exception.NotFoundDataException;
import com.dz.common.utils.IdWorker;
import com.dz.goods.domain.entity.*;
import com.dz.goods.domain.repository.BrandRepository;
import com.dz.goods.domain.repository.CategoryRepository;
import com.dz.goods.domain.repository.SkuRepository;
import com.dz.goods.domain.repository.SpuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : SpuServiceImpl
 * @Description :
 * @Author : ming
 * @Date: 2020-12-03 14:02
 **/
@Service
public class SpuServiceImpl implements SpuService{
    @Autowired
    private SpuRepository spuRepository;
    @Autowired
    private SkuRepository skuRepository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private BrandRepository brandRepository;
    @Autowired
    private IdWorker idWorker;
    @Transactional
    @Override
    public Spu findById(Long id) {
        return spuRepository.findByIdAndDeleteFlag(id,Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value()));
    }
    @Transactional
    @Override
    public Spu add(Spu spu) {
        return spuRepository.saveAndFlush(spu);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {
        Spu spu = spuRepository.findByIdAndDeleteFlag(id,Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value()));
        spu.setDeleteFlag(Integer.valueOf(CodeDomain.DELETE_FLAG_YES.value()));
        spuRepository.saveAndFlush(spu);
    }
    @Transactional
    @Override
    public Spu edit(Spu spu) {
        return spuRepository.saveAndFlush(spu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGoods(Goods goods) throws Exception {
        Spu spu = goods.getSpu();
        spu.setId(idWorker.nextId());
        spuRepository.saveAndFlush(spu);
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku: skuList) {
            sku.setId(idWorker.nextId());
            // name 要生成 (spu的名称+ " "+ 规格的选项的值 )  //  spu的名称: iphonex  规格的数据: spec:{ 颜色:红色,内存大小:16G}
            // 先获取规格的数据
            String spec = sku.getSpec();
            Map<String, String> map = JSON.parseObject(spec, Map.class);
            // 转成map对象  key:颜色  value:红色
            String titile = spu.getName();
            for (String key : map.keySet()) {
                // 获取SPU的名称 拼接 即可
                titile += " " + map.get(key);
            }
            sku.setName(titile);
            // spu_id
            sku.setSpuId(spu.getId());
            // category_id 3级分类的ID
            Long category3Id = spu.getCategory3Id();
            sku.setCategoryId(category3Id);
            Category category = categoryRepository.findById(category3Id).get();
            sku.setCategoryName(category.getName());
            Brand brand = brandRepository.findById(spu.getBrandId()).get();
            sku.setBrandName(brand.getName());
            skuRepository.saveAndFlush(sku);
        }

    }

    @Override
    public Goods findGoodsById(Long spuId) {
        Spu spu = spuRepository.findByIdAndDeleteFlag(spuId,Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value()));
        List<Sku> skuList = skuRepository.findBySpuIdAndDeleteFlag(spu.getId(), Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value()));
        return Goods.builder().spu(spu).skuList(skuList).build();
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void onShelf(Long spuId) throws NotFoundDataException {
        Spu spu = spuRepository.findByIdAndDeleteFlag(spuId,Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value()));
        if (spu==null){
            throw new NotFoundDataException("此商品不存在或已删除!");
        }
        spu.setMarketableFlag(CodeDomain.ON_THE_SHELF.value());
        spuRepository.saveAndFlush(spu);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void offShelf(Long spuId) throws NotFoundDataException {
        Spu spu = spuRepository.findByIdAndDeleteFlag(spuId,Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value()));
        if (spu==null){
            throw new NotFoundDataException("此商品不存在或已删除!");
        }
        if(spu.getMarketableFlag().equals(CodeDomain.OFF_THE_SHELF.value())){
            throw new NotFoundDataException("此商品已下架!");
        }
        spu.setMarketableFlag(CodeDomain.OFF_THE_SHELF.value());
        spuRepository.saveAndFlush(spu);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchOnshelf(List<Long> ids) throws NotFoundDataException {
        List<Spu> spuList = spuRepository.findAllById(ids);
        for (Spu spu:spuList) {
            if (spu==null){
                throw new NotFoundDataException("此商品不存在或已删除!");
            }
            spu.setMarketableFlag(CodeDomain.ON_THE_SHELF.value());
        }
        spuRepository.saveAll(spuList);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchOffshelf(List<Long> ids) throws NotFoundDataException {
        List<Spu> spuList = spuRepository.findAllById(ids);
        for (Spu spu:spuList) {
            if (spu==null){
                throw new NotFoundDataException("此商品不存在或已删除!");
            }
            if(spu.getMarketableFlag().equals(CodeDomain.OFF_THE_SHELF.value())){
                throw new NotFoundDataException("此商品已下架!");
            }
            spu.setMarketableFlag(CodeDomain.OFF_THE_SHELF.value());
        }
        spuRepository.saveAll(spuList);
    }
}
